import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/components/image_view.dart';
import 'package:pornhub_app/components/tab_bar_indicator/easy_fixed_indicator.dart';
import 'package:pornhub_app/generate/app_image_path.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/utils/extension.dart';
import 'package:pornhub_app/views/community/post_publish/post_publish_page.dart';
import 'package:pornhub_app/views/community/publish_video/post_publish_page.dart';

import 'community_page_controller.dart';
import 'community_tab_page.dart';

class CommunityPage extends GetView<CommunityPageController> {
  const CommunityPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CommunityPageController>(builder: (controller) {
      return Scaffold(
        appBar: AppBar(
            automaticallyImplyLeading: false,
            titleSpacing: 0,
            centerTitle: false,
            title: TabBar(
              controller: controller.tabController,
              tabs: controller.tabs,
              isScrollable: true,
              tabAlignment: TabAlignment.start,
              labelStyle: TextStyle(
                fontSize: 17.w,
                fontWeight: FontWeight.w600,
                color: COLOR.white,
              ),
              unselectedLabelStyle: TextStyle(
                fontSize: 17.w,
                fontWeight: FontWeight.w600,
                color: COLOR.color_A1A1A1,
              ),
              indicator: EasyFixedIndicator(
                color: COLOR.color_FF9000,
                width: 20.w,
                height: 3.w,
              ),
              dividerHeight: 0,
            )),
        floatingActionButton: _buildFloatingView(),
        body: _bodyView(),
      );
    });
  }

  _bodyView() {
    return TabBarView(
      controller: controller.tabController,
      children: controller.tabs
          .asMap()
          .entries
          .map(
            (entry) => CommunityTabPage(
              id: entry.key == 0 ? "" : controller.categoryIds[entry.key],
            ).keepAlive,
          )
          .toList(),
    );
  }

  _buildFloatingView() {
    return ImageView(
      src: AppImagePath.community_publish_post,
      width: 50.w,
      height: 50.w,
    ).onOpaqueTap(() async {
      //controller.getCategoryData();
      final result = await showPublishTypeBottomSheet(Get.context!);
      // if (result != null) {
      // 处理选择结果
      // print('Selected type: $result');
      // }
    }).onTap(() async {
      final result = await showPublishTypeBottomSheet(Get.context!);
      // if (result != null) {
      // 处理选择结果
      // print('Selected type: $result');
      // }
    });
  }

// 显示底部弹窗的方法
  Future<void> showPublishTypeBottomSheet(BuildContext context) {
    return showModalBottomSheet(
      context: context,
      backgroundColor: Colors.transparent,
      builder: (BuildContext context) {
        return Container(
          height: 205.w,
          decoration: const BoxDecoration(
            color: Color(0xff212121), // 深色背景
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(16),
              topRight: Radius.circular(16),
            ),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              // 标题栏
              Container(
                padding: EdgeInsets.symmetric(horizontal: 16, vertical: 21),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Spacer(),
                    // 发布类型
                    const Text("发布类型",
                        style: TextStyle(
                          color: Color(0xffffffff),
                          fontWeight: FontWeight.w500,
                          fontStyle: FontStyle.normal,
                          fontSize: 18.0,
                        )),

                    const Spacer(),

                    GestureDetector(
                      onTap: () => Navigator.pop(context),
                      child: const Icon(
                        Icons.close,
                        color: Color(0xff999999),
                        size: 24,
                      ),
                    ),

                    // Spacer(),
                  ],
                ),
              ),
              // 选项区域
              Padding(
                padding:
                    const EdgeInsets.symmetric(horizontal: 0, vertical: 20),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    _buildOptionItem(
                      context: context,
                      icon: Icons.videocam,
                      label: '视频',
                      onTap: () {
                        Get.to( const PostPublishVideoPage(),opaque: false);
                      },
                    ),
                    _buildOptionItem(
                      context: context,
                      icon: Icons.image,
                      label: '图文',
                      onTap: () {
                        Get.to(const PostPublishPage(), opaque: false);
                      },
                    ),
                  ],
                ),
              ),
              const SizedBox(height: 20), // 底部间距
            ],
          ),
        );
      },
    );
  }

// 构建选项项目的辅助方法
  Widget _buildOptionItem({
    required BuildContext context,
    required IconData icon,
    required String label,
    required VoidCallback onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            width: 70,
            height: 70,
            decoration: BoxDecoration(
              color: Color.fromRGBO(47, 47, 47, 1), // 深色背景
              borderRadius: BorderRadius.circular(40),
            ),
            child: Icon(
              icon,
              color: Colors.orange,
              size: 28,
            ),
          ),
          SizedBox(height: 8),
          Text(
            label,
            style: TextStyle(
              color: Colors.white,
              fontSize: 14,
            ),
          ),
        ],
      ),
    );
  }
}
